#include "camerawork.h"
#include <QDateTime>

using namespace cv;
using namespace std;
static cv::Mat zero = cv::Mat::zeros(1, 1, CV_64FC1);

Camerawork::Camerawork(QObject *parent)
{
    //人脸检测模型加载
    std::string detector_param = "/apps/VideoCaptureQt/data/face.param";
    std::string detector_bin = "/apps/VideoCaptureQt/data/face.bin";
    faceDetector = new FaceDetector(detector_param, detector_bin);
}

//检测
int Camerawork::runFacencnn(cv::Mat &image,QImage &img,cv::Mat &faceimage){

    QDateTime start = QDateTime::currentDateTime();


    //人脸识别


    std::vector<Bbox> boxes;
    faceDetector->Detect2(image, boxes);
    if (boxes.size() == 0)
    {
        //不存在人脸 将传入参数置为null
        peopleface.release();
        return -1;
    }
    else if (boxes.size() > 1)
    {
        return -2;
    }

    QPainter painter(&img);
    QPen pen;
    pen.setWidth(6);
    pen.setColor(Qt::green); // 设置画笔色
    painter.setPen(pen); // 设置画笔
    painter.setFont(QFont("Arial",20));
    QRect rect= QRect(boxes[0].x1, boxes[0].y1, boxes[0].x2 - boxes[0].x1 + 1, boxes[0].y2 - boxes[0].y1 + 1);
    painter.drawRect(rect);


//一个是qrect，一个是rect
    cv::Rect rect2= Rect(boxes[0].x1, boxes[0].y1, boxes[0].x2 - boxes[0].x1 + 1, boxes[0].y2 - boxes[0].y1 + 1);
    //保存人脸faceimage


    //--------------------------------------30次触发，发送人脸
    showcount++;
    if(showcount>=60){
        image(rect2).copyTo(faceimage);
        showcount=0;
        emit sendFace(peopleface);
    }

    //-------------------


    QDateTime end = QDateTime::currentDateTime();

    qint64 intervalTimeMS = start.msecsTo(end);
    cout<<"人脸检测time:"<<intervalTimeMS<<endl;

    return 0;
}



//打开摄像头
void Camerawork::openCamera()
{
    capture.set(CV_CAP_PROP_FPS, 15);
    if (capture.isOpened())
        return;

    if (capture.open(200)) {
        cout<<"cam is  opened"<<endl;
    }else {
        cout<<"cam is not opened"<<endl;
        capture.release();
    }

}
//关闭
void Camerawork::closeCamera()
{
    capture.release();
    writer.release();
}

void Camerawork::mainwindowDisplay()
{
    capture >> src_image;
    cv::flip(src_image, src_image, 1);

    if(src_image.data)
    {

        QImage tem = QImage((const unsigned char *)(src_image.data), src_image.cols,
                            src_image.rows, src_image.cols * 3, QImage::Format_RGB888).rgbSwapped();

        QDateTime start = QDateTime::currentDateTime();

        piccount++;
        if(piccount=3){
            piccount=0;
            runFacencnn(src_image,tem,peopleface);
        }



        QDateTime end = QDateTime::currentDateTime();

        qint64 intervalTimeMS = start.msecsTo(end);
        cout<<intervalTimeMS<<endl;


        emit sendPicture(tem);

    }
}
//检测
int Camerawork::runFacencnn2(cv::Mat &image,QImage &img,cv::Mat &faceimage){

    QDateTime start = QDateTime::currentDateTime();


    //人脸识别


    std::vector<Bbox> boxes;
    faceDetector->Detect2(image, boxes);
    if (boxes.size() == 0)
    {
        //不存在人脸 将传入参数置为null
        peopleface.release();
        return -1;
    }
    else if (boxes.size() > 1)
    {
        return -2;
    }

    QPainter painter(&img);
    QPen pen;
    pen.setWidth(6);
    pen.setColor(Qt::green); // 设置画笔色
    painter.setPen(pen); // 设置画笔
    painter.setFont(QFont("Arial",20));
    QRect rect= QRect(boxes[0].x1, boxes[0].y1, boxes[0].x2 - boxes[0].x1 + 1, boxes[0].y2 - boxes[0].y1 + 1);
    painter.drawRect(rect);


    //一个是qrect，一个是rect
    cv::Rect rect2= Rect(boxes[0].x1, boxes[0].y1, boxes[0].x2 - boxes[0].x1 + 1, boxes[0].y2 - boxes[0].y1 + 1);
    //保存人脸faceimage

    image(rect2).copyTo(faceimage);

    //-------------------


    QDateTime end = QDateTime::currentDateTime();

    qint64 intervalTimeMS = start.msecsTo(end);
    cout<<"人脸检测time:"<<intervalTimeMS<<endl;

    return 0;
}

void Camerawork::mainwindowDisplay2()
{
    capture >> src_image;
    cv::flip(src_image, src_image, 1);

    if(src_image.data)
    {

        QImage tem = QImage((const unsigned char *)(src_image.data), src_image.cols,
                            src_image.rows, src_image.cols * 3, QImage::Format_RGB888).rgbSwapped();

        QDateTime start = QDateTime::currentDateTime();

        piccount++;
        if(piccount==3){
            piccount=0;
            runFacencnn2(src_image,tem,peopleface);
        }

        QDateTime end = QDateTime::currentDateTime();

        qint64 intervalTimeMS = start.msecsTo(end);
        cout<<intervalTimeMS<<endl;


        emit sendPicture(tem);

    }
}

void Camerawork::getFace()
{
    if(peopleface.cols!=0){
        emit sendFace(peopleface);
    }
    else{
        cout<<"peopleface is null!"<<endl;
        emit sendString(QString("未检测到人脸"));
    }
//或者发送空信号，代表无人脸-----
}
